<?php
/**
 * User: bim bip
 * Date: 7/27/11
 * Time: 1:58 PM
 */
class M_Comment extends CI_Model{
    var $_movieID;
    var $_content;
    var $_name;
    var $_email;
    var $_time;
    var $_table = 'movie_comment';
    function __construct(){
        parent::__construct();
    }

    function addNew(){
        $obj = array(
            'name' => $this->_name,
            'content' => $this->_content,
            'email' => $this->_email,
            'movie_id' => $this->_movieID,
            'time' => $this->_time
        );
        $this->db->insert($this->_table, $obj);
        //delete cache
        $this->db->cache_delete('comment', 'video');
        return $this->db->insert_id();
    }

    /**
     * @param  $movieID
     * @param  $offset
     * @param  $rowsPerPage
     * @param  $maxPage
     * @return comment array
     */
    function getComment($movieID, $offset, $rowsPerPage, &$maxPage){
        $this->db->cache_on();
        $sql = "SELECT COUNT( * ) AS numrows
                FROM movie_comment
                WHERE movie_id = $movieID";
        $query = $this->db->query($sql);
        $temp = $query->row_array();
        $maxPage = ceil($temp['numrows'] / $rowsPerPage);

        $sql = "SELECT content, name, time
                FROM movie_comment
                WHERE movie_id = $movieID
                ORDER BY TIME DESC
                LIMIT $offset, $rowsPerPage";
        $query = $this->db->query($sql);
        return $query->result_array();
    }
}
 
